package com.shwread.httpsdk.http.face;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shwread.httpsdk.constant.ResultCode;
import com.shwread.httpsdk.http.base.AbstractHttpPostDracom;
import com.shwread.httpsdk.http.callback.ActionListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import logic.bean.AtlasColumnInfo;
import logic.bean.AtlasInfo;
import logic.constants.Const;


/***
 * @author zhangnannan
 */

public class GetAtlasInfoAction
        extends
        AbstractHttpPostDracom
{
	
	private long atlasId;
	private int  pageNum;
	private int  length;
	
	public GetAtlasInfoAction(Context context, long atlasId, int pageNum, int length, ActionListener actionListener) {
		super(context, "qryImageList.do", actionListener);
		this.atlasId = atlasId;
		this.pageNum = pageNum;
		this.length = length;
	}
	
	@Override
	public void getParameter(HashMap<String, String> parameter_map) {
		parameter_map.put("account", Const.phone_number);
		parameter_map.put("timeStamp", String.valueOf(timeStamp));
		parameter_map.put("atlasId", String.valueOf(atlasId));
		parameter_map.put("pageNum", String.valueOf(pageNum));
		parameter_map.put("length", String.valueOf(length));
		parameter_map.put("cid", cid(Const.phone_number + Const.ACCOUNTTYPE + Const.CLIENTTYPE + timeStamp + atlasId + pageNum + length));
		
	}
	
	@Override
	public void doResponseXml(String xml) {
		println(xml);
		int result = -1;
		try
		{
			JSONObject jsonObject = new JSONObject(xml);
			result = jsonObject.getInt("resultCode");
			if (result == 1000)
			{
//				JSONArray ob = jsonObject.getJSONObject("returnObject").getJSONArray("imageList");
//				List<AtlasInfo> atlasList = new ArrayList<AtlasInfo>();
//				for (int i = 0; i < ob.length(); i++)
//				{
//					AtlasInfo infos = new AtlasInfo();
//					JSONObject obj = ob.getJSONObject(i);
//					
//					infos.setAtlasId(obj.getLong("columnId"));
//					infos.setImgId(obj.getLong("imgId"));
//					infos.setShortDesc(obj.getString("shortDesc"));
//					infos.setLongDesc(obj.getString("longDesc"));
//					infos.setImageName(obj.getString("imageName"));
//					infos.setImageUrl(obj.getString("imageUrl"));
//					infos.setImageThumbnailUrl(obj.getString("imageThumbnailUrl"));
//					infos.setImageSize(obj.getInt("imageSize"));
//					infos.setImageWidth(obj.getInt("imageWidth"));
//					infos.setImageHeight(obj.getInt("imageHeight"));
//					infos.setImageThumbnailHeight(obj.getInt("imageThumbnailHeight"));
//					infos.setImageThumbnailWidth(obj.getInt("imageThumbnailWidth"));
//					atlasList.add(infos);
//				}
//				
				
				List<AtlasInfo> atlasList = new Gson().fromJson(jsonObject.getJSONObject("returnObject").getString("imageList"), new TypeToken<List<AtlasInfo>>(){}.getType());
				
				JSONObject obj2 = jsonObject.getJSONObject("returnObject").getJSONObject("page");
				AtlasColumnInfo atlasColumnInfo = new AtlasColumnInfo();
				atlasColumnInfo.setPage(obj2.getInt("page"));
				atlasColumnInfo.setTotalPage(obj2.getInt("totalPage"));
				atlasColumnInfo.setPageNum(obj2.getInt("pageNum"));
				atlasColumnInfo.setTotalNum(obj2.getInt("totalNum"));
				atlasColumnInfo.setAtlasList(atlasList);
				
				listener.OK(atlasColumnInfo);
			}
			else
			{
				listener.ERROR(result, ResultCode.getResultText(result));
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected void doRequestHeader(HashMap<String, String> header_map) {
	}
	
	@Override
	public void dispose() {
	}
	
}
